package com.gs.synedocket.fragment;

import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.gs.synedocket.R;
import com.gs.synedocket.adapter.DocketLogAdapter;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.apiresult.PagedList;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.EventCode;
import com.gs.synedocket.db.bean.Event;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.db.query.DocketQuery;
import com.gs.synedocket.dialog.LoadingDialogUtils;
import com.gs.synedocket.dialog.ToastDialogUtils;
import com.gs.synedocket.fragment.base.BaseListFragment;
import com.gs.synedocket.presenter.DocketListPresenter;
import com.gs.synedocket.util.AppUtil;
import com.gs.synedocket.util.DocketUtil;
import com.gs.synedocket.util.SPUtil;
import com.gs.synedocket.util.ToastUtil;
import com.gs.synedocket.view.IDocketListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class DocketListFragment extends BaseListFragment<IDocketListView, DocketListPresenter, DocketDto, DocketQuery> implements IDocketListView {

    private DocketLogAdapter adapter;
    private TextView tvRvTitle;
    private long lastSyncTime;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_docket_list;
    }

    @Override
    protected DocketListPresenter createPresenter() {
        return new DocketListPresenter();
    }

    @Override
    protected DocketQuery createQuery() {
        return new DocketQuery();
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        tvRvTitle = fd(R.id.tv_rv_title);
        adapter = new DocketLogAdapter(context, list, R.layout.rvitem_docket_summary);
        adapter.setOnItemClickListener((v, position) -> {
            try {
                if (DocketUtil.getInstance().getUpgradeLevel() == AllConstants.UPGRADE_FIRST) {
                    ToastUtil.showToast(R.string.upgrade_first);
                    LogUtil.getInstance().insertOrReplaceData(getString(R.string.upgrade_log, AppUtil.getInstance().getVersion()));
                    return;
                }
                LogUtil.getInstance().insertOrReplaceData("查看送貨清單" + list.get(position).getDocketNo());
                DocketUtil.getInstance().setLogDetail(false);
                DocketUtil.getInstance().setDocketId(list.get(position).getId());
                EventBus.getDefault().post(new Event(EventCode.EVENT_ON_HAND));
            } catch (Exception ignored) {

            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        mPresenter.dockets(query);
    }

    @Override
    protected void initData() {
        showLoading();
        list.clear();
        query.init();
        query.setTruckNo(SPUtil.getInstance().getString(AllConstants.TRUCK_NAME));
        mPresenter.dockets(query);
    }

    @Override
    public void onGetDataSuccess(ApiResultData<PagedList<DocketDto>> apiResultData) {
        if (apiResultData.getStatus() == 2000) {
            list.addAll(apiResultData.getData().getList());
        } else {
            ToastDialogUtils.getInstance().showInfoMessage(context, apiResultData.getMessage());
        }
        adapter.notifyDataSetChanged();
        tvRvTitle.setText(getString(R.string.total, apiResultData.getData().getTotalCount()));
        if (smartRefreshLayout.getTag() != null && smartRefreshLayout.getTag().equals(AllConstants.REFRESH)
                && System.currentTimeMillis() - lastSyncTime > 3000L) {
            EventBus.getDefault().post(new Event(EventCode.EVENT_LIST_REFRESH_DOWNLOAD));
            lastSyncTime = System.currentTimeMillis();
        }
        refreshSmartRefreshLayout();
    }

    @Override
    public void onGetDataFail(Exception e) {
        refreshSmartRefreshLayout();
        ToastDialogUtils.getInstance().showInfoMessage(context, R.string.network_error);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == EventCode.EVENT_LIST_DOWNLOAD_SUCCESS || event.getWhat() == EventCode.EVENT_CHANGE_TRUCK) {
            list.clear();
            query.init();
            query.setTruckNo(SPUtil.getInstance().getString(AllConstants.TRUCK_NAME));
            mPresenter.dockets(query);
        }
    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(context);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData();
        }
    }
}

